package cn.kai.ymcc.mq.consumer;

import cn.kai.ymcc.domain.MessageSms;
import cn.kai.ymcc.domain.MessageStation;
import cn.kai.ymcc.dto.MessageSendDto;
import cn.kai.ymcc.service.IMessageStationService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * @Author 12469
 * @Date 2022/7/25 23:20
 * @PackageName:cn.kai.ymcc.mq.consumer
 * @ClassName: MessageConsumer
 * @Description:发站内信的消费者
 * @Version 1.0
 */

@Component
@RocketMQMessageListener(
        consumerGroup="stationmsg-consumer-group",
        topic="sendmsg-topic",
        selectorExpression="station-tags"
)

@Slf4j
public class StationMessageConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IMessageStationService messageStationService;

    @Override
    public void onMessage(MessageExt messageExt) {
        //取出dto
        String jsonStr = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        MessageSendDto dto = JSONObject.parseObject(jsonStr, MessageSendDto.class);

        //保存站内信至数据库
        MessageStation messageStation = new MessageStation();
        BeanUtils.copyProperties(dto,messageStation);
        messageStationService.insert(messageStation);

    }
}
